package com.applancer.fragment;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;

import com.applancer.fragment.NavigationDrawerFragment.NavigationDrawerCallbacks;
import com.applancer.nativecmsflip.InfoActivity;
import com.applancer.nativecmsflip.R;
import com.applancer.nativecmsflip.SettingActivity;
import com.applancer.nativecmsflip.UserActivity;
import com.applancer.provider.StringUtils;

public class RightMenuFragment extends Fragment {

	protected ListView menu;
	protected SharedPreferences sp;
	protected View contentview;
	protected DrawerLayout mDrawerLayout;
	protected ActionBarDrawerToggle mDrawerToggle;
	private NavigationDrawerCallbacks mCallbacks;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		sp = PreferenceManager.getDefaultSharedPreferences(getActivity());

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		setHasOptionsMenu(true);

		initial();
		execution();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mCallbacks = (NavigationDrawerCallbacks) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					"Activity must implement NavigationDrawerCallbacks.");
		}
	}

	private void initial() {

	}

	private void execution() {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				contentview.getContext(), android.R.layout.simple_list_item_1,
				android.R.id.text1, contentview.getContext().getResources()
						.getStringArray(R.array.menu_right));

		menu.setAdapter(adapter);

		menu.setOnItemClickListener(onClickEvent);
	}

	OnItemClickListener onClickEvent = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			if (!isAdded())
				return;
			// Account
			// Notification
			// Follow post
			// Contact
			// Info

			switch (position) {
			case 0:
				Intent i = new Intent(contentview.getContext(),
						UserActivity.class);
				startActivity(i);

				getActivity().overridePendingTransition(R.anim.slide_left_in,
						R.anim.slide_left_out);
				break;
			case 1:
				Intent setting_intent = new Intent(contentview.getContext(),
						SettingActivity.class);
				startActivity(setting_intent);

				getActivity().overridePendingTransition(R.anim.slide_left_in,
						R.anim.slide_left_out);
				break;
			case 2:
				if (mDrawerLayout != null) {
					mDrawerLayout.closeDrawer(Gravity.END);
				}

				if (mCallbacks != null) {
					mCallbacks.onNavigationDrawerItemSelected(
							getString(R.string.bookmark_id),
							getString(R.string.book_mark));
				}
				break;
			case 3:
				Intent info_intent = new Intent(contentview.getContext(),
						InfoActivity.class);
				startActivity(info_intent);

				getActivity().overridePendingTransition(R.anim.slide_left_in,
						R.anim.slide_left_out);
			default:
				break;
			}
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		contentview = inflater.inflate(R.layout.right_menu_fragment, null);
		menu = (ListView) contentview.findViewById(R.id.rmenu);
		menu.setVerticalScrollBarEnabled(false);

		fixListView();

		return contentview;
	}

	public void fixListView() {
		boolean hasMenuKey = ViewConfiguration.get(getActivity())
				.hasPermanentMenuKey();
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

		if (currentapiVersion >= 19) {
			lp.setMargins(0, StringUtils.getStatusBarHeight(getActivity())
					+ StringUtils.getActionbarHeight(getActivity()), 0, 0);
			menu.setLayoutParams(lp);
		}

		menu.setPadding(0, 0, 0,
				(!hasMenuKey ? StringUtils.getNavigationBarHeigh(getActivity())
						: 0));
	}

	public void showMenu() {
		if (mDrawerLayout.isDrawerOpen(Gravity.END)) {
			mDrawerLayout.closeDrawer(Gravity.END);
		} else {
			mDrawerLayout.openDrawer(Gravity.END);
			mDrawerLayout.closeDrawer(Gravity.START);
		}
	}

	public void setUp(int fragmentId, DrawerLayout drawerLayout) {
		mDrawerLayout = drawerLayout;

		// set up the drawer's list view with items and click listener

		ActionBar actionBar = getActivity().getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);

		// ActionBarDrawerToggle ties together the the proper interactions
		// between the navigation drawer and the action bar app icon.
		mDrawerToggle = new ActionBarDrawerToggle(getActivity(), /* host Activity */
		mDrawerLayout, /* DrawerLayout object */
		R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
		R.string.navigation_drawer_open, /*
										 * "open drawer" description for
										 * accessibility
										 */
		R.string.navigation_drawer_close /*
										 * "close drawer" description for
										 * accessibility
										 */
		) {
			@Override
			public void onDrawerClosed(View drawerView) {
				super.onDrawerClosed(drawerView);
				if (!isAdded()) {
					return;
				}

				// getActivity().invalidateOptionsMenu(); // calls
				// onPrepareOptionsMenu()
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				super.onDrawerOpened(drawerView);
				if (!isAdded()) {
					return;
				}

			}
		};

		// If the user hasn't 'learned' about the drawer, open it to introduce
		// them to the drawer,
		// per the navigation drawer design guidelines.
		// if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
		// mDrawerLayout.openDrawer(mFragmentContainerView);
		// }

		// Defer code dependent on restoration of previous instance state.
		mDrawerLayout.post(new Runnable() {
			@Override
			public void run() {
				mDrawerToggle.syncState();
			}
		});

		mDrawerLayout.setDrawerListener(mDrawerToggle);
	}
}
